<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="line-height: 36px;">员工基本信息维护</span>
        <el-button style="float: right;" type="primary" @click="dialogOpen('add')">新增员工</el-button>
      </div>
      <s-table
        :table="table"
        @http="tableWatch">
        <el-table-column
          fixed="right"
          align="center"
          slot="operation"
          class-name="operationMore"
          label="操作"
          width="120">
          <template scope="s">
            <div v-if="s.row.iscancel === 2">
              <div v-if="s.row.bxSupplierState !== 5">
                <el-button type="primary" v-if="s.row.bxSupplierState === 1" size="mini" @click="tableUpload(s.row)" round>提交</el-button>
                <el-button type="warning" size="mini" @click="dialogSearchOpen(s.row)" v-if="s.row.bxSupplierState !== 1" round>轨迹</el-button>
                <el-button type="danger" size="mini" v-if="s.row.bxSupplierState === 2 " style="margin-left: 5px" @click="tableRevert(s.row)" round>撤回</el-button>
                <el-tooltip placement="top">
                  <div slot="content">更多可用操作</div>
                  <el-popover
                    popper-class="demo-table-expand"
                    placement="right"
                    v-if="s.row.bxSupplierState !== 2"
                    title="更多可用操作"
                    trigger="click">
                    <slot name="content">
                      <div>
                        <el-button type="success" icon="el-icon-edit" v-if="s.row.bxSupplierState === 1 || s.row.bxSupplierState === 4" size="mini" @click="dialogOpen('PUT', s.row)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="s.row.bxSupplierState === 4 && USER.auth.indexOf('resources_employee_disable')!==-1"  @click="tableLock(s.row)">禁用</el-button>
                        <el-button type="danger" v-if="USER.auth.indexOf('resources_employee_delete')!==-1 && s.row.bxSupplierState === 1" icon="el-icon-delete" size="mini" @click="tableDelete(s.row)">删除</el-button>
                      </div>
                    </slot>
                    <el-button slot="reference"
                               icon="el-icon-more"
                               round
                               size="mini"
                               class="__reference"></el-button>
                  </el-popover>
                </el-tooltip>
              </div>
              <div v-else>
                <el-button type="success" size="mini" @click="dialogOpen('PUT', s.row)" round>编辑</el-button>
                <el-tooltip placement="top">
                  <div slot="content">更多可用操作</div>
                  <el-popover
                    popper-class="demo-table-expand"
                    placement="right"
                    v-if="s.row.bxSupplierState !== 2"
                    title="更多可用操作"
                    trigger="click">
                    <slot name="content">
                      <div>
                        <el-button type="primary" size="mini" @click="tableUpload(s.row)">提交</el-button>
                        <el-button type="warning" size="mini" @click="dialogSearchOpen(s.row)">轨迹</el-button>
                      </div>
                    </slot>
                    <el-button slot="reference"
                               icon="el-icon-more"
                               round
                               size="mini"
                               class="__reference"></el-button>
                  </el-popover>
                </el-tooltip>
              </div>
            </div>
            <div v-else>
              <el-button type="success" size="mini" v-if="USER.auth.indexOf('resources_employee_disable')!==-1" @click="tableLock(s.row)" round>启用员工</el-button>
            </div>
          </template>
        </el-table-column>
        <el-form-item label="姓名" prop="bxSupplierName">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierName"
                    placeholder="姓名" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="流程状态" prop="bxSupplierState">
          <el-select v-model="autoVue_table_bxSupplierState" placeholder="流程状态">
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option
              label="已驳回"
              :value="5">
            </el-option>
            <el-option
              label="已审核"
              :value="4">
            </el-option>
            <el-option
              label="审核中"
              :value="3">
            </el-option>
            <el-option
              label="待审核"
              :value="2">
            </el-option>
            <el-option
              label="草稿"
              :value="1">
            </el-option>
          </el-select>
        </el-form-item>
        <!--
        <el-form-item label="账户名称" prop="bxSupplierAccount">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierAccount"
                    placeholder="账户名称" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="bxSupplierBank">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierBank"
                    placeholder="开户行" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="银行账号" prop="bxSupplierBankaccount">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierBankaccount"
                    placeholder="银行账号" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="bxSupplierPhone">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierPhone"
                    placeholder="联系方式" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="bxSupplierSelfno">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierSelfno"
                    placeholder="身份证号" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="结算周期" prop="bxSupplierSetlecircle">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierSetlecircle"
                    placeholder="结算周期" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="bxSupplierDepartmentname">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierDepartmentname"
                    placeholder="所属部门" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="bxSupplierState">
          <el-input type="text"
                    class="input"
                    v-model="autoVue_table_bxSupplierState"
                    placeholder="状态" spellcheck="false" auto-capitalize="off" auto-complete="off"
                    auto-correct="off"></el-input>
        </el-form-item>
        -->
      </s-table>
    </el-card>
    <s-dialog :dialog="dialog">
      <p slot="title">{{dialog.title}}员工 <span
        v-if="dialog.title === '修改'">员工名称：{{dialog.row.bxSupplierName}}</span></p>
      <s-form :form="formDialog" ref="formDialog">
        <el-row>
          <el-col :xs="24" :lg="{span: 7, offset: 0}" class="">
            <el-form-item prop="bxSupplierName" label="员工姓名">
              <el-input
                type="text"
                v-model="formDialog.bxSupplierName"
                placeholder="员工姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="bxSupplierSelfno" label="身份证号">
              <el-input
                type="text"
                v-model="formDialog.bxSupplierSelfno"
                placeholder="身份证号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="bxSupplierPhone" label="联系方式">
              <el-input
                type="text"
                v-model="formDialog.bxSupplierPhone"
                placeholder="联系方式"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :lg="{span: 7, offset: 0}" class="">
            <el-form-item prop="bxSupplierDepartmentid" label="所属部门">
              <el-cascader
                :options="dialog.bxSupplierDepartmentid"
                v-model="formDialog.bxSupplierDepartmentid"
                :show-all-levels="false"
                ref="bxSupplierDepartmentid"
                clearable
                filterable
                change-on-select
                :props="{
                  value: 'key',
                }"
                placeholder="所属部门"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="workflowConfigId" label="所属流程">
              <el-select
                filterable
                v-model="formDialog.workflowConfigId"
                placeholder="请选择">
                <el-option
                  v-for="item in dialog.workflowConfigId"
                  :key="item.id"
                  :label="item.processName + '   ' +  item.departmentName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="bxSupplierSetlecircle" label="结算周期">
              <el-select
                filterable
                v-model="formDialog.bxSupplierSetlecircle"
                placeholder="请选择">
                <el-option
                  v-for="item in dialog.bxSupplierSetlecircle"
                  :key="item.id"
                  :label="item.text"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :lg="{span: 7, offset: 0}" class="">
            <el-form-item label="账户名称">
              <el-input
                type="text"
                disabled
                v-model="formDialog.bxSupplierName"
                placeholder="账户名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="bxSupplierBank" label="开户行">
              <el-input
                type="text"
                v-model="formDialog.bxSupplierBank"
                placeholder="开户行"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 7, offset: 1}" class="">
            <el-form-item prop="bxSupplierBankaccount" label="银行账号">
              <el-input
                type="text"
                v-model="formDialog.bxSupplierBankaccount"
                placeholder="银行账号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </s-form>
      <el-button slot="footer" @click="dialogSave">保存</el-button>
    </s-dialog>
    <s-dialog :dialog="dialogUpload">
      <p slot="title">申请审核员工 <span v-if="dialogUpload.row.bxSupplierName">员工名称：{{dialogUpload.row.bxSupplierName}}</span></p>
      <s-form :form="formDialogUpload" ref="formDialogUpload">
        <el-row>
          <el-col :xs="24" :lg="{span: 23, offset: 0}" class="">
            <el-form-item prop="userId" label="审核人">
              <el-select v-model="formDialogUpload.userId"
                         filterable
                         placeholder="请选择">
                <el-option
                  v-for="item in dialogUpload.userId"
                  :key="item.id"
                  :label="item.name + '   ' + item.orgName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </s-form>
      <el-button slot="footer" @click="dialogUploadSave">保存</el-button>
    </s-dialog>
    <process ref="process"></process>
  </div>
</template>

<script>
  import {AutoVue, AutoPromise} from '~/plugins/Tools';

  function __form () {
    return {
      id: { value: '', rule: [], cRemarks: '' },
      bxSupplierName: { value: '', rule: [ 'vNull' ], cRemarks: '' },
      bxSupplierSelfno: { value: '', rule: [ 'vNull', 'v_IDCard' ], cRemarks: '员工' },
      bxSupplierPhone: { value: '', rule: ['vNull'], cRemarks: '' },
      bxSupplierDepartmentname: { value: '', rule: ['vNull'], cRemarks: '' },
      bxSupplierDepartmentid: { value: [], rule: [ 'vNull__change' ], cRemarks: '' },
      bxSupplierSetlecircle: { value: '', rule: [ 'vNull__change' ], cRemarks: '' },
      bxSupplierAccount: { value: '', rule: [], cRemarks: '' },
      bxSupplierBank: { value: '', rule: [ 'vNull' ], cRemarks: '' },
      bxSupplierBankaccount: { value: '', rule: [ 'vNull' ], cRemarks: '' },
      workflowConfigId: { value: '', rule: [ 'vNull__change' ], cRemarks: '' },
      bxSupplierState: { value: '', rule: [], cRemarks: '' },
    };
  }
  function __formDialogUpload () {
    return {
      userId: { value: '', rule: ['vNull'], cRemarks: '' },
    };
  }

  function treefilter (arrayTree) {
    if (typeof arrayTree === 'undefined') return undefined;
    let tree = [];
    arrayTree.forEach(item => {
      if (item.children && item.children.length === 0) {
        delete item.children;
      } else {
        item.children = treefilter(item.children);
      }
      delete item.disabled;
      tree.push(item);
    });
    return tree;
  }

  export default AutoVue({
    data: {
      table: {
        column: [
          { label: '员工姓名',
            prop: 'bxSupplierName',
            minWidth: '0px',
            cRemarks: '',
            search: true },
          { label: '身份证号',
            prop: 'bxSupplierSelfno',
            minWidth: '120px',
            cRemarks: '员工',
            search: true },
          { label: '联系方式',
            prop: 'bxSupplierPhone',
            minWidth: '100px',
            cRemarks: '',
            search: true },
          { label: '所属部门',
            prop: 'bxSupplierDepartmentname',
            minWidth: '220px',
            cRemarks: '',
            search: true },
          { label: '流程状态',
            prop: 'bxSupplierState',
            minWidth: '0px',
            elTag: {
              filterType (type) {
                switch (type) {
                case 1:
                  return 'info';
                case 2:
                  return 'info';
                case 3:
                  return 'primary';
                case 4:
                  return 'success';
                case 5:
                  return 'danger';
                }
              },
            },
            formatter: (row, column) => {
              let step = parseInt(row[column.property || column]);
              switch (step) {
              case 1:
                return '草稿';
              case 2:
                return '待审核';
              case 3:
                return '审核中';
              case 4:
                return '已审核';
              case 5:
                return '已驳回';
              }
            },
            cRemarks: '',
            search: true },
          { label: '结算周期',
            prop: 'show_bxSupplierSetlecircle',
            minWidth: '0px',
            cRemarks: '',
            search: true },
          { label: '账户名称',
            prop: 'bxSupplierAccount',
            minWidth: '0px',
            cRemarks: '',
            search: true },
          { label: '开户行',
            prop: 'bxSupplierBank',
            minWidth: '0px',
            cRemarks: '',
            search: true },
          { label: '银行账号',
            prop: 'bxSupplierBankaccount',
            minWidth: '0px',
            cRemarks: '',
            search: true },
        ],
        key: 'id',
        url: '/empolyee/searchByPage',
        tableColumn: 5,
        muti: false,
      },
      formDialog: __form(),
      dialog: {
        title: '新增',
        bxSupplierDepartmentid: [],
        bxSupplierSetlecircle: [],
        workflowConfigId: [],
        width: '80%',
      },
      dialogUpload: {
        row: {},
        ids: [],
        userId: [],
      },
      formDialogUpload: __formDialogUpload(),
    },
    transition: 'slide-fade-bottom',
    head: {
      'title': '员工基本信息维护',
      'meta': [
        {'hid': 'description', 'name': 'description', 'content': '乐速科技平台 资源管理 查看员工基本信息维护'},
      ],
    },
    store: ['USER'],
    methods: {
      tableRevert (row) {
        this.$prompt('撤回此员工原因', '确认撤回？', {
          confirmButtonText: '是',
          cancelButtonText: '否',
        }).then(async ({value}) => {
          await this.post('/workflowConfig/revokeWorkflowProcess', {
            id: row.id,
            auditorNote: value,
          });
          this.tableWatch('table');
        });
      },
      tableDelete (row) {
        let tableRef = 'table';

        let ids = [];
        if (row) {
          ids.push(row.id);
        } else {
          if (this[tableRef].multipleSelection.length < 1) {
            return this.$alert('您还未选择，请您至少选择一个！', '提示');
          }
          this[tableRef].multipleSelection.forEach(item => {
            ids.push(item.id);
          });
        }
        this.$confirm('您将删除员工。', '确认删除？', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning',
        }).then(async () => {
          await this.del('/empolyee', {
            data: ids,
          });
          this.tableWatch(tableRef);
        });
      },
      tableLock (row) {
        let tableRef = 'table';
        this.$confirm(`您将${row.iscancel === 2 ? '禁用' : '启用'}员工。`, '是否确认？', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning',
        }).then(async () => {
          await this.get('/empolyee/iscancel/' + row.id);
          this.tableWatch(tableRef);
        });
      },
      tableUpload (row) {
        let tableRef = 'table';

        let ids = [];
        if (row) {
          ids.push(row.id);
        } else {
          if (this[tableRef].multipleSelection.length < 1) {
            return this.$alert('您还未选择，请您至少选择一个！', '提示');
          }
          this[tableRef].multipleSelection.forEach(item => {
            ids.push(item.id);
          });
        }
        this.dialogUploadOpen(ids, row);
      },

      async dialogOpen (type, more) {
        if (type) {
          type = type.toUpperCase();
        } else return;
        await AutoPromise([
          async () => {
            let json = await this.get('/dictionaries/getDepartmentTree');
            this.dialog.bxSupplierDepartmentid = treefilter(json);
          },
          async () => {
            this.dialog.bxSupplierSetlecircle = await this.get('/setlecvcle/setlecycleList');
          },
          async () => {
            let {contentList} = await this.post('/workflowConfig/searchByPage', {
              'pageNumber': 1,
              'pageSize': 9999,
              'processType': 1,
            });
            this.dialog.workflowConfigId = contentList;
          },
          async () => {
            if (more && !more.bxSupplierDepartmentidOld) {
              more.bxSupplierDepartmentidOld = await this.get('/dictionaries/departmentIds/' + more.bxSupplierDepartmentid);
            }
          },
        ]);
        switch (type) {
        case 'ADD':
          this.dialog.title = '新增';
          this.formDialog.id = 0;
          break;
        case 'PUT':
          this.dialog.title = '修改';
          this.$nextTick(() => {
            this.formDialog = {
              ...this.formDialog,
              ...this.ObjectClone(__form(), more),
            };
            this.formDialog.bxSupplierDepartmentid = more.bxSupplierDepartmentidOld;
          });
          this.dialog.row = more;
          break;
        }
        this.dialog.visible = true;
      },
      dialogSave () {
        if (this.dialog.title === '新增') {
          this.VALIDATE('formDialog', async option => {
            option.bxSupplierAccount = option.bxSupplierName;
            option.bxSupplierDepartmentid = option.bxSupplierDepartmentid[option.bxSupplierDepartmentid.length - 1];
            option.bxSupplierDepartmentname = this.$refs.bxSupplierDepartmentid.currentLabels.join('-');
            try {
              await this.post('/empolyee', option);
              this.dialog.dialogInit.call(this);
              this.tableWatch('table');
              this.dialog.visible = false;
            } catch (err) {
              console.log(err);
            }
          });
        } else {
          this.VALIDATE('formDialog', async option => {
            option.bxSupplierAccount = option.bxSupplierName;
            option.id = this.dialog.row.id;
            option.bxSupplierDepartmentid = option.bxSupplierDepartmentid[option.bxSupplierDepartmentid.length - 1];
            option.bxSupplierDepartmentname = this.$refs.bxSupplierDepartmentid.currentLabels.join('-');
            try {
              await this.put('/empolyee', option);
              this.dialog.dialogInit.call(this);
              this.tableWatch('table');
              this.dialog.visible = false;
            } catch (err) {
              console.log(err);
            }
          });
        }
      },

      async dialogUploadOpen (ids, row) {
        let {contentList} = await this.get('/workflowConfig/selectAuditors/' + row.id);
        this.dialogUpload.userId = contentList;
        this.dialogUpload.row = row;
        this.dialogUpload.ids = ids;
        this.dialogUpload.visible = true;
      },
      dialogUploadSave () {
        this.VALIDATE('formDialogUpload', async option => {
          try {
            await this.post('/empolyee/readyreview', {
              ...option,
              id: this.dialogUpload.row.id,
            });
            this.dialogUpload.dialogInit.call(this);
            this.tableWatch('table');
          } catch (err) {
            console.error(err);
          }
        });
      },
      dialogSearchOpen (row) {
        this.$refs.process.open(row);
      },
    },
    mounted: function () {
      this.tableWatch('table');
      console.log(this);
    },
  });
</script>
